package org.caowei.samples.dao;

import java.util.List;

import org.caowei.samples.model.ExamUserRole;
import org.caowei.samples.model.User;
import org.caowei.samples.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserDAOImpl extends BaseHibernateDAO<User> implements IUserDAO {
	
	@Autowired
	private IUserDAO IUserDAO;
	@Autowired
	private IUserRoleDAO IUserRoleDAO;
	
	@Override
	public List<User> findAllUsers() {
		return this.loadAll();
	}

	@Override
	public User getUserById(String id) {
		return this.get(id);

	}

	@Override
	public void createUser(User user) throws Exception {
		if (user != null) {
			this.save(user);
		}
	}

	@Override
	public void editUser(User user) throws Exception {
		if (user != null) {
			this.update(user);
		}
	}

	@Override
	public void deleteUser(String id) throws Exception {
		User exsitsuser = this.get(id);
		if (exsitsuser != null) {
			this.delete(exsitsuser);
		}
	}

	@Override
	public UserVO getloginById(String id) {
		User entity = IUserDAO.getUserById(id);
		ExamUserRole role = IUserRoleDAO.findRole(id);
		if (entity != null) {
			UserVO vo = new UserVO();
			BeanUtils.copyProperties(entity, vo);
			vo.setRoleId(role.getRoleId());
			return vo;
		}
		return null;
	}

	@Override
	public List<User> getAllStudent() {
		return this.loadAll();
	}
	
}
